package ch01.SL;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AccessPersonMethods {
    public static void main(String[] args) {
        try{
            Class clz = Class.forName("reflect.entity.Person");

            Method getAge = clz.getDeclaredMethod("getAge",null);
            getAge.setAccessible(true);
            Object returnAge = getAge.invoke(null,null);
            System.out.println("年龄是："+returnAge);

            Object person = clz.newInstance();

            Method silentMethod = clz.getDeclaredMethod("silentMethod",null);
            silentMethod.setAccessible(true);
            silentMethod.invoke(person,null);

            Method setName = clz.getDeclaredMethod("setName",String.class);
            setName.invoke(person,"张三");
            Object retyrnName = clz.getDeclaredMethod("getName").invoke(person);

            System.out.println("刚才设定的name是："+retyrnName);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}
